package com.lanou.projecta.news.game;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.projecta.R;
import com.lanou.projecta.WebViewActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/13.
 */
public class NewsGame extends Fragment{
    private String url = "http://c.3g.163.com/nc/article/headline/T1348654151579/0-20.html";
    private GridView gridView;
    private RequestQueue  queue;
    private GameBase gameBase;
    private List<GameBase.T1348654151579Entity> base;
    private GameAdapter adapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news_game,null);
        queue = Volley.newRequestQueue(getActivity());
        gridView = (GridView) view.findViewById(R.id.game_gridView);

        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                base = new ArrayList<>();
                Gson gson = new Gson();
                gameBase = gson.fromJson(response,GameBase.class);
                base = gameBase.getT1348654151579();
                base.remove(0);
                adapter = new GameAdapter(base,getActivity());
                gridView.setAdapter(adapter);
                gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        String url = base.get(i).getUrl_3w();
                        Intent intent = new Intent(getActivity(), WebViewActivity.class);
                        intent.putExtra("webView",url);
                        startActivity(intent);
                    }
                });

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(request);

        return view;
    }
}
